package com.res.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.res.entity.Computer;
import com.res.service.ComputerService;
import com.res.util.FenyeUtil;

@RestController
@RequestMapping("/computer")
public class ComputerController {
	@Autowired
	private ComputerService computerService;

	@GetMapping("/get")
	public Object getAll(Computer c, Integer cid, Integer limit, Integer page) {
		Pageable pageable = PageRequest.of(page - 1, 10);
		FenyeUtil<Computer> f = new FenyeUtil<Computer>();
		f.setData(computerService.getComputerAll(c, pageable, cid).getContent());
		f.setCount((int) computerService.getComputerAll(c, pageable, cid).getTotalElements());
		return f;
	}

	// 添加方法
	@PostMapping
	public Object addComputer(Computer c, Integer cid) {
		return computerService.addComputer(c, cid);
	}

	// 修改方法
	@PutMapping
	public Object updateComputer(Computer c, Integer cid) {
		return computerService.updateComputer(c, cid);
	}

	// 删除方法
	@DeleteMapping
	public Object deleteComputer(Integer cid) {
		return computerService.deleteComputer(cid);
	}
}
